package com.zackeus.xupdate.entity;

import androidx.annotation.NonNull;

import com.zackeus.common.utils.StringUtils;
import com.zackeus.xupdate.XUpdate;
import com.zackeus.xupdate.constant.Error;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

/**
 * 更新异常
 * @name: UpdateError
 * @author: zz
 * @date: 2022/11/18
 */
public class UpdateError extends Throwable {

    /* 错误码 */
    @Error.Unit
    private final int mCode;

    public UpdateError(@Error.Unit int code) {
        this(code, "");
    }

    public UpdateError(@Error.Unit int code, String message) {
        super(make(code, message));
        this.mCode = code;
    }

    public UpdateError(@Error.Unit int code, Throwable e) {
        super(e);
        this.mCode = code;
    }

    private static String make(@Error.Unit int code, String message) {
        String m = XUpdate.with().errorMessages().get(code);
        if (StringUtils.isBlank(m)) {
            return "";
        }
        if (StringUtils.isBlank(message)) {
            return m;
        }
        return m + "(" + message + ")";
    }

    @NonNull
    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}
